package com.hengpeng.itfin.service.impl;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;

import com.hengpeng.common.constant.Constant;
import com.hengpeng.itfin.common.MessageUtil;
import com.hengpeng.itfin.common.SystemConfigUtil;
import com.hengpeng.itfin.constants.SystemConfigConstants;
import com.hengpeng.itfin.domain.partner.PartnerProcessor;
import com.hengpeng.itfin.domain.partner.PartnerProcessorFactory;
import com.hengpeng.itfin.service.PartnerService;
import com.hengpeng.itfinbase.message.RequestMessage;
import com.hengpeng.itfinbase.message.ResponseMessage;
import com.hengpeng.itfinbase.message.partner.AddBidInfoRequest;
import com.hengpeng.itfinbase.message.partner.BindCardRequest;
import com.hengpeng.itfinbase.message.partner.CashRequestMsg;
import com.hengpeng.itfinbase.message.partner.CorpOpenRequestMsg;
import com.hengpeng.itfinbase.message.partner.CorpRegisterQueryRequest;
import com.hengpeng.itfinbase.message.partner.CreditAssignRequest;
import com.hengpeng.itfinbase.message.partner.DrawReconciliationRequestMsg;
import com.hengpeng.itfinbase.message.partner.InvestInfoRequestMsg;
import com.hengpeng.itfinbase.message.partner.LoansRequest;
import com.hengpeng.itfinbase.message.partner.ModifyRequest;
import com.hengpeng.itfinbase.message.partner.NotifyRequestMsg;
import com.hengpeng.itfinbase.message.partner.NotifyResponseMsg;
import com.hengpeng.itfinbase.message.partner.OpenRequestMsg;
import com.hengpeng.itfinbase.message.partner.PartnerTransType;
import com.hengpeng.itfinbase.message.partner.PayRequestMsg;
import com.hengpeng.itfinbase.message.partner.QueryAcctsRequestMsg;
import com.hengpeng.itfinbase.message.partner.QueryBalanceRequest;
import com.hengpeng.itfinbase.message.partner.QueryCardInfoRequestMsg;
import com.hengpeng.itfinbase.message.partner.QueryTransStatRequest;
import com.hengpeng.itfinbase.message.partner.ReconciliationRequestMsg;
import com.hengpeng.itfinbase.message.partner.RepaymentRequestMsg;
import com.hengpeng.itfinbase.message.partner.SaveReconciliationRequestMsg;
import com.hengpeng.itfinbase.message.partner.TenderCancleRequest;
import com.hengpeng.itfinbase.message.partner.TransferRequest;
import com.hengpeng.itfinbase.message.partner.TrfReconciliationRequestMsg;
import com.hengpeng.itfinbase.message.partner.UsrUnFreezeRequest;
import com.hengpeng.itfinbase.persist.Partner;
import com.hengpeng.itfinbase.persist.Partner.PartnerType;

@Lazy
@Service
public class PartnerServiceImpl extends BaseServiceImpl implements PartnerService
{
	private static final Log logger = LogFactory.getLog(PartnerServiceImpl.class);

	private static Partner partner;

	private PartnerProcessor getProcessor(PartnerType partnerType)
	{
		if (partnerType != null) {
			partner = daoFactory.getPartnerDao().getPartnerByType(partnerType);
		} else {
			partner = daoFactory.getPartnerDao().get(SystemConfigUtil.getConfigValue(SystemConfigConstants.PARTNER_ID));
		}
		return PartnerProcessorFactory.getProcessor(serviceFactory, partner);
	}

	@Override
	public boolean isTrustPattern()
	{
		if (partner == null)
		{
			partner = daoFactory.getPartnerDao().get(SystemConfigUtil.getConfigValue(SystemConfigConstants.PARTNER_ID));
		}
		boolean rb = true;
		if (partner.getPartnerType().equals(PartnerType.FUND))
		{
			rb = false;
		}
		return rb;
	}

	@Override
	public NotifyResponseMsg dealNotify(NotifyRequestMsg notifyMsg)
	{
		NotifyResponseMsg res = new NotifyResponseMsg();
		try
		{
			String cmdId = notifyMsg.getParameter().get("CmdId");
			if (cmdId == null || cmdId.equals(""))
			{
				cmdId = notifyMsg.getParameter().get("RespType");
			}
			PartnerTransType type = PartnerTransType.getPartnerTransType(cmdId);
			switch (type)
			{
				case UserRegister:
					res = getProcessor(notifyMsg.getPartnerType()).openNotify(notifyMsg);
					break;
				case CorpRegister:
					res = getProcessor(notifyMsg.getPartnerType()).corpOpenNotify(notifyMsg);
					break;
				case UserBindCard:
					res = getProcessor(notifyMsg.getPartnerType()).bindCardNotify(notifyMsg);
					break;
				case AcctModify:
					break;
				case Transfer:
					res = getProcessor(notifyMsg.getPartnerType()).transferNotify(notifyMsg);
					break;
				case NetSave:
					res = getProcessor(notifyMsg.getPartnerType()).payNotify(notifyMsg);
					break;
				case Cash:
					res = getProcessor(notifyMsg.getPartnerType()).drawNotify(notifyMsg);
					break;
				case BatchRepayment:
					res = getProcessor(notifyMsg.getPartnerType()).batchRepaymentNotify(notifyMsg);
					break;
				case Repayment:
					res = getProcessor(notifyMsg.getPartnerType()).repaymentNotify(notifyMsg);
					break;
				case InitiativeTender:
					res = getProcessor(notifyMsg.getPartnerType()).initiativeTenderNotify(notifyMsg);
					break;
				case Loans:
					res = getProcessor(notifyMsg.getPartnerType()).loansNotify(notifyMsg);
					break;
				case UsrUnFreeze:
					res = getProcessor(notifyMsg.getPartnerType()).usrUnFreezeNotify(notifyMsg);
					break;
				case CreditAssign:
					res = getProcessor(notifyMsg.getPartnerType()).creditAssignNotify(notifyMsg);
					break;
			}
		}
		catch (Exception e)
		{
			logger.error(e);
			return MessageUtil.getErrorResponseMessage(NotifyResponseMsg.class,
					Constant.TRANSACTION_RESPONSE_CODE_UNKOWN, "系统异常,异常信息：" + e.getMessage());
		}
		return res;
	}

	@Override
	public ResponseMessage request(RequestMessage reqMsg)
	{
		ResponseMessage res = new ResponseMessage();
		try
		{
			beforeRequest(reqMsg);
			switch (reqMsg.getPartnerTransType())
			{
				case QueryAccts:
					res = getProcessor(reqMsg.getPartnerType()).queryAcctsRequest((QueryAcctsRequestMsg) reqMsg);
					break;
				case UserRegister:
					res = getProcessor(reqMsg.getPartnerType()).packageOpenRequest((OpenRequestMsg) reqMsg);
					break;
				case CorpRegister:
					res = getProcessor(reqMsg.getPartnerType()).packageCorpOpenRequest((CorpOpenRequestMsg) reqMsg);
					break;
				case CorpRegisterQuery:
					res = getProcessor(reqMsg.getPartnerType()).queryCorpOpenRequest((CorpRegisterQueryRequest) reqMsg);
					break;
				case UserBindCard:
					res = getProcessor(reqMsg.getPartnerType()).packageBindCardRequest((BindCardRequest) reqMsg);
					break;
				case AcctModify:
					res = getProcessor(reqMsg.getPartnerType()).packageModifyRequest((ModifyRequest) reqMsg);
					break;
				case Transfer:
					res = getProcessor(reqMsg.getPartnerType()).packageTransferRequest((TransferRequest) reqMsg);
					break;
				case NetSave:
					res = getProcessor(reqMsg.getPartnerType()).packagePayRequest((PayRequestMsg) reqMsg);
					break;
				case Cash:
					res = getProcessor(reqMsg.getPartnerType()).packageDrawRequest((CashRequestMsg) reqMsg);
					break;
				case BatchRepayment:
					res = getProcessor(reqMsg.getPartnerType()).batchRepaymentRequest((RepaymentRequestMsg) reqMsg);
					break;
				case Repayment:
					res = getProcessor(reqMsg.getPartnerType()).repaymentRequest((RepaymentRequestMsg) reqMsg);
					break;
				case QueryBalanceBg:
					res = getProcessor(reqMsg.getPartnerType()).queryBalanceBgRequest((QueryBalanceRequest) reqMsg);
					break;
				case InitiativeTender:
					res = getProcessor(reqMsg.getPartnerType()).packageInitiativeTenderRequest((InvestInfoRequestMsg) reqMsg);
					break;
				case AddBidInfo:
					res = getProcessor(reqMsg.getPartnerType()).addBidInfo((AddBidInfoRequest) reqMsg);
					break;
				case Loans:
					res = getProcessor(reqMsg.getPartnerType()).loans((LoansRequest) reqMsg);
					break;
				case UsrUnFreeze:
					res = getProcessor(reqMsg.getPartnerType()).usrUnFreeze((UsrUnFreezeRequest) reqMsg);
					break;
				case TenderCancle:
					res = getProcessor(reqMsg.getPartnerType()).tenderCancle((TenderCancleRequest) reqMsg);
					break;
				case QueryTransStat:
					res = getProcessor(reqMsg.getPartnerType()).queryTransStat((QueryTransStatRequest) reqMsg);
					break;
				case QueryCardInfo:
					res = getProcessor(reqMsg.getPartnerType()).queryCardInfo((QueryCardInfoRequestMsg) reqMsg);
					break;
				case Reconciliation:
					res = getProcessor(reqMsg.getPartnerType()).reconciliation((ReconciliationRequestMsg) reqMsg);
					break;
				case TrfReconciliation:
					res = getProcessor(reqMsg.getPartnerType()).trfReconciliation((TrfReconciliationRequestMsg) reqMsg);
					break;
				case SaveReconciliation:
					res = getProcessor(reqMsg.getPartnerType()).saveReconciliation((SaveReconciliationRequestMsg) reqMsg);
					break;
				case CashReconciliation:
					res = getProcessor(reqMsg.getPartnerType()).drawReconciliation((DrawReconciliationRequestMsg) reqMsg);
					break;
				case CreditAssign:
					res = getProcessor(reqMsg.getPartnerType()).packageCreditAssign((CreditAssignRequest) reqMsg);
					break;
			}
		}
		catch (Exception e)
		{
			logger.error(e);
			return MessageUtil.getErrorResponseMessage(ResponseMessage.class,
					Constant.TRANSACTION_RESPONSE_CODE_UNKOWN, "系统异常,异常信息：" + e.getMessage());
		}
		return res;
	}

	@Override
	public void beforeRequest(RequestMessage request)
	{
		switch (request.getPartnerTransType())
		{
			case Transfer:
				serviceFactory.getTransferService().packTransferRequest((TransferRequest) request);
				break;
			case NetSave:
				serviceFactory.getPaymentService().packPayRequest((PayRequestMsg) request);
				break;
			case Cash:
				serviceFactory.getPaymentService().packDrawRequest((CashRequestMsg) request);
				break;
			case BatchRepayment:
				serviceFactory.getRepaymentService().packBatchRepayment((RepaymentRequestMsg) request);
				break;
			case Repayment:
				serviceFactory.getRepaymentService().packRepayment((RepaymentRequestMsg) request);
				break;
			case InitiativeTender:
				serviceFactory.getInvestInfoService().packInvestRequest((InvestInfoRequestMsg) request);
				break;
			case CreditAssign:
				serviceFactory.getAssignService().packCreditAssing((CreditAssignRequest) request);
				break;
		}
	}
}
